<?php

class PersonasController extends AppController {

	var $name = 'Personas';
	var $helpers = array('Html', 'Form');



	function index2() {
		$this->Persona->recursive = 0;
		$this->set('personas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Persona->recursive = 0;
			$this->Session->setFlash(__('Invalid Persona.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('persona', $this->Persona->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Persona->create();
			if ($this->Persona->save($this->data)) {
				$this->Session->setFlash(__('The Persona has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Persona could not be saved. Please, try again.', true));
			}
		}
		$empresas = $this->Persona->Empresa->find('list', array('conditions' => array('Empresa.vigencia' => '1')));
		$previsions = $this->Persona->Prevision->find('list', array('conditions' => array('Prevision.vigencia' => '1')));
		$tipo_documentos = $this->Persona->Tipo->getTiposDocumentos();
		$tipo_sexo = $this->Persona->Tipo->getTiposSexo();
		$tipo_estado_civil = $this->Persona->Tipo->getTiposEstadoCivil();
		$tipo_nacionalidad = $this->Persona->Tipo->getTiposNacionalidad();
		$tipo_actividad = $this->Persona->Tipo->getTiposActividad();
		
		
		$this->set(compact('empresas', 'previsions'));
		$this->set('tipo_documentos',$tipo_documentos);
		$this->set('tipo_sexo',$tipo_sexo);
		$this->set('tipo_estado_civil',$tipo_estado_civil);
		$this->set('tipo_nacionalidad',$tipo_nacionalidad);
		$this->set('tipo_actividad',$tipo_actividad);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Persona', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Persona->save($this->data)) {
				$this->Session->setFlash(__('The Persona has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Persona could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Persona->read(null, $id);
		}
		$empresas = $this->Persona->Empresa->find('list');
		$previsions = $this->Persona->Prevision->find('list');
		$this->set(compact('empresas','previsions'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Persona', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Persona->del($id)) {
			$this->Session->setFlash(__('Persona deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


        function search() {

            if(!empty($this->data)){
            $url['action'] = 'index';


            foreach($this->data as $k=>$v){
                foreach ($v as $kk=>$vv){ $url[$k.'.'.$kk]=$vv; }
            }

            $this->redirect($url, null, true);

            }

        }

        function index() {
         

        	$this->Persona->recursive = -1;
                //
                // filter by identificacion
                //
                if(isset($this->passedArgs['Search.identificacion'])) {

                        // set the conditions
                        $this->paginate['conditions'][]['Persona.identificacion'] = $this->passedArgs['Search.identificacion'];

                        // set the Search data, so the form remembers the option
                        $this->data['Search']['identificacion'] = $this->passedArgs['Search.identificacion'];

                }

                //
                // filter by paterno
                //
                if(isset($this->passedArgs['Search.paterno'])) {
                        $this->paginate['conditions'][]['Persona.paterno LIKE'] = str_replace('*','%',$this->passedArgs['Search.paterno']);

                        $this->data['Search']['paterno'] = $this->passedArgs['Search.paterno'];

                }

                //
                // filter by materno
                //
                if(isset($this->passedArgs['Search.materno'])) {
                        $this->paginate['conditions'][]['Persona.materno LIKE'] = str_replace('*','%',$this->passedArgs['Search.materno']);

                        $this->data['Search']['materno'] = $this->passedArgs['Search.materno'];

                }

                //
                // filter by nombres
                //
                if(isset($this->passedArgs['Search.nombres'])) {
                        $this->paginate['conditions'][]['Persona.nombres LIKE'] = str_replace('*','%',$this->passedArgs['Search.nombres']);
                        $this->data['Search']['nombres'] = $this->passedArgs['Search.nombres'];

                }

                // get posts
                //$this->Post->contain($contain); // required if you are using Containable
                //$this->paginate['reset']=false; // required if you are using Containable
                $personas = $this->paginate();

                // set the category path of each post (not required, just an example)
                // requires Category to have the Tree behaviour
                //
                // you can use this to add anything you want to the $posts array
                // before it is sent to the view


                $this->set(compact('personas'));
        }







}
?>
